package com.hxzy.testutils;

import com.hxzy.pojo.FastDFSFile;
import org.csource.fastdfs.*;
import org.csource.common.NameValuePair;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.ClassPathResource;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class FastDFSClient {

    private static org.slf4j.Logger logger = LoggerFactory.getLogger(FastDFSClient.class);
    /*** * 初始化加载FastDFS的TrackerServer配置 */
    static {
        try {
            String filePath = new ClassPathResource("fdfs_client.conf").getFile().getAbsolutePath();
            ClientGlobal.init(filePath);
        } catch (Exception e) {
            logger.error("FastDFS Client Init Fail!", e);
        }
    }

    /*** * 文件上传 * @param file * @return */
    public static String[] upload(FastDFSFile file) {
        //获取文件的作者
        NameValuePair[] meta_list = new NameValuePair[1];
        meta_list[0] = new NameValuePair("author", file.getAuthor());
        //接收返回数据
        String[] uploadResults = null;
        StorageClient storageClient = null;

        try {
            //创建StorageClient客户端对象
            storageClient = getTrackerClient();
            /*** * 文件上传
             * 1)文件字节数组
             * 2)文件扩展名 *
             * 3)文件作者
             */
            uploadResults = storageClient.upload_file(file.getBytes(), file.getExt(), meta_list);

        } catch (Exception e) {
            logger.error("Exception when uploadind the file:" + file.getName(), e);
            if (uploadResults == null && storageClient != null) {
                logger.error("upload file fail, error code:" + storageClient.getErrorCode());
            }
        }
        //获取组名
        String groupName = uploadResults[0];
        System.out.println(groupName);
        //获取文件存储路径
        String remoteFileName = uploadResults[1];
        System.out.println(remoteFileName);
        return uploadResults;
    }

    /*** * 获取文件信息 * @param groupName:组名 * @param remoteFileName：文件存储完整名 * @return */

    public static FileInfo getFile(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getTrackerClient();
            return storageClient.get_file_info(groupName, remoteFileName);
        } catch (Exception e) {
            logger.error("Exception: Get File from Fast DFS failed", e);
        }
        return null;
    }

    /*** * 文件下载 * @param groupName * @param remoteFileName * @return */

    public static InputStream downFile(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getTrackerClient();
            byte[] bytes = storageClient.download_file(groupName, remoteFileName);
            ByteArrayInputStream ins = new ByteArrayInputStream(bytes);
            return ins;
        } catch (Exception e) {
            logger.error("Exception: Get File from Fast DFS failed", e);
            return null;

        }
    }

    /*** * 文件删除 * @param groupName * @param remoteFileName * @throws Exception */
    public static void deleteFile(String groupName, String remoteFileName) {
        try {
            StorageClient storageClient = getTrackerClient();
            int i = storageClient.delete_file(groupName, remoteFileName);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /*** * 获取Storage信息,IP和端口 * @param groupName * @param remoteFileName * @return * @throws IOException */
    public static StorageServer[] getStoreStorages(String groupName) {
        TrackerClient trackerClient = new TrackerClient();
        try {
            TrackerServer trackerServer = trackerClient.getConnection();
            return trackerClient.getStoreStorages(trackerServer, groupName);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }

    }

    /*** * 获取Storage信息,IP和端口 * @param groupName * @param remoteFileName * @return * @throws IOException */

    public static ServerInfo[] getFetchStorages(String groupName, String remoteFileName) {
        TrackerClient trackerClient = new TrackerClient();
        try {
            TrackerServer trackerServer = trackerClient.getConnection();
            return trackerClient.getFetchStorages(trackerServer, groupName, remoteFileName);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }
    /*** * 获取Tracker服务地址
     * @return
     * @throws IOException
     */
    public static String getTrackerUrl(){
        try {
            return "http://"+getTrackerServer().getInetSocketAddress().getHostString()+":"+ ClientGlobal.getG_tracker_http_port()+"/";
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /*** * 获取Storage客户端
     * @return
     * @throws IOException
     */
    public static StorageClient getTrackerClient() throws IOException {
        TrackerServer trackerServer = getTrackerServer();
        StorageClient storageClient = new StorageClient(trackerServer, null);
        return storageClient;
    }

    /*** *
     * 获取Tracker
     * @return
     * @throws IOException
     */
    private static TrackerServer getTrackerServer() throws IOException {
        TrackerClient trackerClient = new TrackerClient();
        TrackerServer trackerServer = trackerClient.getConnection();
        return trackerServer;
    }

}
